package com.amap.map2d.demo.basic;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.UiSettings;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.map2d.demo.R;

/**
 * @Auther: Timmy
 * @Data: 2017/8/4
 * @Fuction:
 */

public class MyTestMapActivity extends Activity implements View.OnClickListener {

    private static final String TAG = MyTestMapActivity.class.getSimpleName();
    private MapView mMapView;
    private AMap aMap;
    private AMapLocationClient mMapLocationClient;


    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);

    private static final int TRAN_COLOR = Color.argb(0, 0, 0, 0);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mytest_activity);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);// 此方法必须重写

        Button locationButton = (Button) findViewById(R.id.button);
        Button startButton = (Button) findViewById(R.id.start);
        Button endButton = (Button) findViewById(R.id.end);
        locationButton.setOnClickListener(this);
        startButton.setOnClickListener(this);
        endButton.setOnClickListener(this);

        init();
    }

    private void onClickLocation() {

        if (mMapLocation != null) {
            if (mMapLocation.getErrorCode() == 0) {
                //取出经纬度
                LatLng latLng = new LatLng(mMapLocation.getLatitude(), mMapLocation.getLongitude());

                //然后可以移动到定位点,使用animateCamera就有动画效果
                aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 19f));
            }
        }

    }

    private void init() {
        if (aMap == null) {
            aMap = mMapView.getMap();
            setUpMap();
        }
    }

    private void setUpMap() {
        aMap.setLocationSource(mLocationSource);// 设置定位监听
        UiSettings uiSettings = aMap.getUiSettings();
        uiSettings.setZoomControlsEnabled(false);
        uiSettings.setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        setupLocationStyle();
    }

    private void setupLocationStyle() {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标

        ImageView iv = new ImageView(this);
        FrameLayout.LayoutParams fmIv = new FrameLayout.LayoutParams(120, 120);
        iv.setImageResource(R.drawable.gps_point);
        iv.setLayoutParams(fmIv);

        //myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point));
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromView(iv));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(TRAN_COLOR);
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(5);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(TRAN_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上

        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);

        aMap.setMyLocationStyle(myLocationStyle);
    }

    private LocationSource.OnLocationChangedListener mChangedListener;
    private AMapLocationClientOption mMapLocationClientOption;

    private LocationSource mLocationSource = new LocationSource() {
        /**
         * 激活定位
         */
        @Override
        public void activate(OnLocationChangedListener onLocationChangedListener) {

            mChangedListener = onLocationChangedListener;

            if (mMapLocationClient == null) {
                mMapLocationClient = new AMapLocationClient(MyTestMapActivity.this);
                mMapLocationClientOption = new AMapLocationClientOption();
                //设置定位监听
                mMapLocationClient.setLocationListener(mMapLocationListener);

                // ========================
                //设置为高精度定位模式
                mMapLocationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
                //设置定位间隔,单位毫秒,默认为2000ms
                mMapLocationClientOption.setInterval(1000);
                //设置是否返回地址信息（默认返回地址信息）
                mMapLocationClientOption.setNeedAddress(true);
                //设置是否只定位一次,默认为false
                mMapLocationClientOption.setOnceLocation(false);
                //设置是否强制刷新WIFI，默认为强制刷新
                mMapLocationClientOption.setWifiScan(true);
                //设置是否允许模拟位置,默认为false，不允许模拟位置
                mMapLocationClientOption.setMockEnable(false);

                // --------------------------------

                //设置定位参数
                mMapLocationClient.setLocationOption(mMapLocationClientOption);
                // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
                // 注意设置合适的定位时间的间隔（最小间隔支持为2000ms），并且在合适时间调用stopLocation()方法来取消定位请求
                // 在定位结束后，在合适的生命周期调用onDestroy()方法
                // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
                mMapLocationClient.startLocation();

                aMap.moveCamera(CameraUpdateFactory.zoomTo(19));
            }
        }

        // 停止定位
        @Override
        public void deactivate() {
            mMapLocationListener = null;
            if (mMapLocationClient != null) {
                mMapLocationClient.stopLocation();
                mMapLocationClient.onDestroy();
            }
            mMapLocationClient = null;
        }
    };

    private AMapLocation mMapLocation;

    private AMapLocationListener mMapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            mMapLocation = aMapLocation;

            if (null != mChangedListener) {
                if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                    mChangedListener.onLocationChanged(aMapLocation);

                    /**

                     double lat = aMapLocation.getLatitude();
                     double lon = aMapLocation.getLongitude();

                     // 设置当前地图显示为当前位置
                     // aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lon), 19));

                     MarkerOptions markerOptions = new MarkerOptions();
                     markerOptions.position(new LatLng(lat, lon));
                     markerOptions.visible(true);
                     BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.location_marker));
                     markerOptions.icon(bitmapDescriptor);

                     aMap.clear();
                     aMap.addMarker(markerOptions);
                     */
                }
            }
        }
    };

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
        mLocationSource.deactivate();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
        if (null != mMapLocationClient) {
            mMapLocationClient.onDestroy();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                onClickLocation();
                break;
            case R.id.start:
                Toast.makeText(this, "开始", Toast.LENGTH_SHORT).show();
                // 开始记录轨迹
                // 1. 添加一个开始的market点

                // 2. 同时开始记录轨迹
                break;
            case R.id.end:
                Toast.makeText(this, "停止", Toast.LENGTH_SHORT).show();
                // 停止记录轨迹

                // 停止记录轨迹

                break;
        }
    }
}
